<?php
/**
 * Created by PhpStorm.
 * User: River
 * Email: river0428@gmail.com
 * Date: 2018/6/11
 * Time: 下午5:08
 */

namespace Modules\Admin\Auth\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use App\Models\AdminUser;

class AdminUserController extends Controller
{
    public function index()
    {
        return view('admin.auth::user', \Request::all());
    }

    public function all(Request $request)
    {
        $limit  = $request->input('limit', 10);
        $offset = $request->input('offset', 0);

        $query = AdminUser::query();
        $query->when($request->input('trashed') == 1, function ($q) {
            return $q->onlyTrashed();
        });
        $data['total'] = $query->count();
        $data['items'] = $query->with('role')->take($limit)->skip($offset)
            ->orderBy('id', 'asc')->get();

        return success($data);
    }

    public function store(Request $request)
    {
        $data = $request->only(['username', 'password', 'mobile','role_id', 'status', 'kefu_no']);

        $validator = Validator::make($data, [
            'username' => 'required|unique:admin_user,username|max:250',
            'password' => 'required|min:6, max:128',
            //'mobile' => 'required|min:11, max:11',
        ]);
        if ($validator->fails()) {
            return fail(400, $validator->errors()->all());
        }
        $data['password'] = Hash::make($data['password']);

        $user = AdminUser::create($data);
        return success($user);
    }

    public function update(Request $request, $id)
    {
        $user = AdminUser::find($id);
        if (!$user) {
            return fail('资源未找到', [], 404);
        }

        $data      = $request->only(['username', 'password','mobile', 'role_id', 'status', 'kefu_no']);
        $validator = Validator::make($data, [
            'username' => [
                Rule::unique('admin_user')->ignore($id),
            ],
            'password' => 'min:6, max:128',
            'mobile'   => 'required'
        ]);

        if ($validator->fails()) {
            return fail(400, $validator->errors()->all());
        }

        if ($request->has('username')) {
            $user->username = $data['username'];
        }
        if ($request->has('password')) {
            $user->password = Hash::make($data['password']);
        }
        if ($request->has('mobile')) {
            $user->mobile = $data['mobile'];
        }
        if ($request->has('role_id')) {
            $user->role_id = $data['role_id'];
        }
        if ($request->has('status')) {
            $user->status = $data['status'];
        }
        if ($request->has('kefu_no')) {
            $user->kefu_no = $data['kefu_no'];
        }

        $user->save();

        return success($user);
    }

    public function destroy($id)
    {
        $user = AdminUser::find($id);
        $user->delete();
        return success();
    }

    public function restore($id)
    {
        $user = AdminUser::onlyTrashed()->find($id);
        if ($user) {
            $user->restore();
        }
        return success();
    }
}